<template>
    <!-- 对话框表单 -->
    <el-dialog custom-class="ba-operate-dialog" :close-on-click-modal="false"
    :model-value="baTable.form.operate == 'giveCoupon'" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form ref="formRef" @keyup.enter="baTable.onSubmit(formRef)" :model="baTable.form.items"
                    label-position="right" :label-width="baTable.form.labelWidth + 'px'" :rules="rules"
                    v-if="!baTable.form.loading">
                    <FormItem type="remoteSelect" label="会员" v-model="baTable.form.items!.ids" placeholder="请选择会员"
                        :input-attr="{
                            multiple: true,
                            params: { isTree: true, search: [{ field: 'status', val: 'enable', operator: '=' }] },
                            field: 'nickname',
                            pk: 'user.id',
                            'remote-url': userUser + 'index',
                        }" />
                    <FormItem type="remoteSelect" label="优惠券" v-model="baTable.form.items!.coupon_ids" placeholder="优惠券"
                        :input-attr="{
                            multiple: true,
                            params: { isTree: true },
                            field: 'coupon_name',
                            pk: 'coupon.id',
                            'remote-url': coupon + 'index',
                        }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                    type="primary">
                    {{ t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import type { ElForm } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { coupon } from '/@/api/controllerUrls'
import { userUser } from '/@/api/controllerUrls'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

// baTable.onSubmit = function(){
//     console.log(baTable.form.items)
//     baTable.api.postData(baTable.form.operate!, baTable.form.items!).then(() => {
//         baTable.onTableHeaderAction('refresh', {})
//         baTable.form.submitLoading = false
//         this.form.operateIds?.shift()
//         baTable.toggleForm()
//     }).catch(() => {
//         baTable.form.submitLoading = false
//     })
// }
</script>

<style scoped lang="scss">
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--el-border-color);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}

.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}

.avatar {
    width: 110px;
    height: 110px;
    display: block;
}

.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
